<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-4" @click="router.go(-1)"> 返回 </a-button>
        <a-descriptions class="mt-3" :column="4">
          <a-descriptions-item label="单号"> {{ formState.bsdh || '-' }}</a-descriptions-item>
          <a-descriptions-item label="出库仓库"> {{ formState.ckmc || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单人">{{ formState.qrr || empName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单时间">{{ formState.addtime || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-between items-center mb-2">
        <div class="flex items-center">
          <BaseInput
            v-model:value="tcCode"
            auto-mark
            :auto-focus="autoFocus"
            placeholder="请扫码"
            maxlength="20"
            class="w-64 mr-4"
            @change="onChangeCode"
            @keydown.enter="onSaveData"
          />
          <DrugInfo :value="{ mc: drugInfo.ypmc, gg: drugInfo.gg, cj: drugInfo.cj, ph: drugInfo.ph, yxq: drugInfo.expirationTime }" />
        </div>
        <div class="flex">
          <a-button type="primary" @click="openDelete">扫码移除</a-button>
        </div>
      </div>
      <BaseTable :data-source="tableData" column-code="000761" :loading="loading" max-fill :extra-height="64" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="record" />
          </template>
          <template v-if="column.dataIndex === 'yy'">
            <EditCell>
              <a-input v-model:value="record.bsyy" placeholder="请输入" @blur="editRow(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onOpenView(record)">查看追溯码</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <DeleteTraceModal v-model:open="deleteOpen" mode="3" :row-data="{ ...formState, formSn: formState.bsdh }" @ok="getData()" />
    <ViewTrace v-model:open="viewTraceOpen" :row-data="{ ...rowData, formSn: rowData.bsdh }" />
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'
import EditCell from '@/components/EditCell'
import DeleteTraceModal from '../components/DeleteTraceModal'
import ViewTrace from '../components/ViewTrace.vue'
import DrugInfo from '../components/DrugInfo.vue'

const formState = ref({})
const route = useRoute()
const router = useRouter()
const deleteOpen = ref(false)
const viewTraceOpen = ref(false)
const tcCode = ref('')
const rowData = ref({})
const drugInfo = ref({})
const autoFocus = ref(false)

const { empName } = useUserStore()

const editRow = (row) => !row.isNew && fetchSave(row)
const fetchSave = async (row) => {
  const { code, msg } = await post('/drugStorage/psiform/doSaveLossOverflowDetail', row)
  if (code !== 0) return message.error(msg || '操作失败')
  getData()
  message.destroy()
  message.success('修改成功')
}

const onSaveData = async () => {
  if (!tcCode.value) return
  autoFocus.value = false
  const { code, msg, data } = await post('/drugStorage/psiform/doSaveTcCodeDrug', {
    formSn: route.query?.bsdh,
    operationMode: 3,
    tcCode: tcCode.value,
    drugStoreId: formState.value.ckbh
  })
  autoFocus.value = true
  tcCode.value = ''
  if (code !== 0) return message.error(msg || '操作失败')
  drugInfo.value = data || {}
  getData()
}

const getHeaderData = async () => {
  const { data, code } = await post('/drugStorage/psiform/getLossOverflowHeadList', {
    mcbm: route.query?.bsdh
  })
  if (code === 0) {
    formState.value = data[0]
  }
}

const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getLossOverflowDetailList',
  immediate: false,
  formatParams: () => {
    return {
      bsdh: route.query?.bsdh
    }
  }
})

const openDelete = (row) => {
  rowData.value = row
  deleteOpen.value = true
}
const onOpenView = (row) => {
  rowData.value = row
  viewTraceOpen.value = true
}
const onChangeCode = () => {
  drugInfo.value = {}
}

watchEffect(() => {
  if (route.query?.bsdh) {
    getHeaderData()
    getData()
  }
})
</script>
<style lang="less" scoped>
:deep(.ant-card-head) {
  padding-left: 8px;
}
</style>
